package cn.iune.web.platform.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.iune.platform.dictionary.IExpressInfoService;
import cn.iune.platform.dictionary.IRegionInfoService;
import cn.iune.platform.dictionary.ITradeInfoService;
import cn.iune.platform.dictionary.RegionInfo;

@Controller
@RequestMapping(value="/backcenter")
public class BackstageController {

	@Autowired
	private IRegionInfoService regionInfoService;
	@Autowired
	private ITradeInfoService tradeInfoService;
	@Autowired
	private IExpressInfoService expressInfoService;



	//区域信息
	@RequestMapping(value="/regionInfo/tree",method=RequestMethod.GET)
	public ModelAndView regionInfoView(){
		ModelAndView mav = new ModelAndView("/backcenter/regionInfo/tree");
		return mav;
	}

	
	
	@RequestMapping(value="/regionInfo/tree/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject regionInfoQuery(@RequestBody JSONObject data) {
		return regionInfoService.queryData(data);
	}

	@RequestMapping(value="/regionInfo/tree/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject regionInfoSave(@RequestBody JSONObject data) {
		return regionInfoService.saveData(data);
	}
	
	@RequestMapping(value="/regionInfo/tree/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject regionInfoDelete(@RequestBody JSONObject data) {
		return regionInfoService.deleteData(data);
	}
	
	@RequestMapping(value="/regionInfo/tree/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject regionInfoCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject json = regionInfoService.newData(data);
		return json;
	}
	
	
	
	//应用设置排序
	@RequestMapping(value="/regionInfo/sortView",method=RequestMethod.GET)
	public ModelAndView regionInfoSortView(){
		ModelAndView mav = new ModelAndView("/backcenter/regionInfo/sort");
		return mav;
	}
	//排序
	@RequestMapping(value = "/regionInfo/sort", method = RequestMethod.POST)
	@ResponseBody
	public void regionInfoSort(@RequestBody JSONObject data){
		String parentID = data.getString("parentID");
		JSONArray jsonArray =  data.getJSONArray("ids");
//		System.out.println("jsonArray-->"+jsonArray);
		List<String> ids = new ArrayList<String>();
		for(int i=0; i<jsonArray.size();i++){
			ids.add(jsonArray.getString(i));
		}
//		System.out.println("ids-->"+ids);
		regionInfoService.sortGroupInfo(parentID, ids);
	}

	
	
	
	
	
	//获取大区信息
	@RequestMapping(value = "/regionInfo/getRegionArray", method = RequestMethod.GET)
	@ResponseBody
	public JSONArray getRegionArray() {
		List<RegionInfo> objs = regionInfoService.getRoots();
		JSONArray array = new JSONArray();
		
//		JSONObject jsonNull = new JSONObject();
//		jsonNull.put("id","?");
//		jsonNull.put("name", "-- 选择省 --");
//		array.add(jsonNull);

		for(RegionInfo obj : objs){
			JSONObject json = new JSONObject();
			json.put("id", obj.getfID());
			json.put("code", obj.getRegionCode());
			json.put("name", obj.getRegionName());
			json.put("fullName", obj.getRegionFullName());
			array.add(json);
		}
		return array;
	}

	//获取xx信息
	@RequestMapping(value = "/regionInfo/getDistrictArray", method = RequestMethod.POST)
	@ResponseBody
	public JSONArray getDistrictArray(@RequestBody JSONObject data) {
		String fullName = data.getString("fullName");
		String nodeType = data.getString("nodeType");
		//System.out.println("fullName-->"+fullName);
		List<RegionInfo> objs = regionInfoService.getChildsByFullName(fullName,nodeType);
		JSONArray array = new JSONArray();
		
//		JSONObject jsonNull = new JSONObject();
//		jsonNull.put("id","?");
//		jsonNull.put("name", "-- 选择省 --");
//		array.add(jsonNull);

		for(RegionInfo obj : objs){
			JSONObject json = new JSONObject();
			json.put("id", obj.getfID());
			json.put("code", obj.getRegionCode());
			json.put("name", obj.getRegionName());
			json.put("fullName", obj.getRegionFullName());
			array.add(json);
		}
		/*
		for(RegionInfo obj : objs){
			array.add(obj.getRegionName());
		}
		*/
		return array;
	}

	
	
	
	//获取省份信息
	@RequestMapping(value = "/regionInfo/getProvinceArray", method = RequestMethod.GET)
	@ResponseBody
	public JSONArray getProvinceArray() {
		List<RegionInfo> objs = regionInfoService.getProvinces();
		JSONArray array = new JSONArray();
		
//		JSONObject jsonNull = new JSONObject();
//		jsonNull.put("id","?");
//		jsonNull.put("name", "-- 选择省 --");
//		array.add(jsonNull);

		for(RegionInfo obj : objs){
			JSONObject json = new JSONObject();
			json.put("id", obj.getfID());
			json.put("code", obj.getRegionCode());
			json.put("name", obj.getRegionName());
			json.put("fullName", obj.getRegionFullName());
			String group = obj.getRegionFullName();
			group = group.replaceFirst("/", "");
			int endIndex = group.indexOf("/");
			group = group.substring(0, endIndex);
			json.put("group", group);
			array.add(json);
		}
		/*
		for(RegionInfo obj : objs){
			array.add(obj.getRegionName());
		}
		*/
		return array;
	}

	//获取城市或者区县
	@RequestMapping(value ={"/regionInfo/getCityArray"}, method = RequestMethod.GET)
	@ResponseBody
	public JSONArray getCityArray(@RequestParam(value="id",required=true) String id) {
		List<RegionInfo> objs = regionInfoService.getChilds(id);
		JSONArray array = new JSONArray();
		JSONObject jsonNull = new JSONObject();
		jsonNull.put("id","?");
		jsonNull.put("name", "-- 选择市 --");
		array.add(jsonNull);

		for(RegionInfo obj : objs){
			JSONObject json = new JSONObject();
			json.put("id", obj.getfID());
			json.put("code", obj.getRegionCode());
			json.put("name", obj.getRegionName());
			json.put("fullName", obj.getRegionFullName());
			String group = obj.getRegionFullName();
			group = group.replaceFirst("/", "");
			int endIndex = group.indexOf("/");
			group = group.substring(0, endIndex);
			json.put("group", group);
			array.add(json);
		}
		
		return array;
		
	}

	//获取城市或者区县
	@RequestMapping(value ={"/regionInfo/getZoneArray"}, method = RequestMethod.GET)
	@ResponseBody
	public JSONArray getZoneArray(@RequestParam(value="id",required=true) String id) {
		List<RegionInfo> objs = regionInfoService.getChilds(id);
		JSONArray array = new JSONArray();
		JSONObject jsonNull = new JSONObject();
		jsonNull.put("id","?");
		jsonNull.put("name", "-- 选择区/县 --");
		array.add(jsonNull);

		for(RegionInfo obj : objs){
			JSONObject json = new JSONObject();
			json.put("id", obj.getfID());
			json.put("code", obj.getRegionCode());
			json.put("name", obj.getRegionName());
			json.put("fullName", obj.getRegionFullName());
			String group = obj.getRegionFullName();
			group = group.replaceFirst("/", "");
			int endIndex = group.indexOf("/");
			group = group.substring(0, endIndex);
			json.put("group", group);
			array.add(json);
		}
		
		return array;
		
	}
	
	
	//行业分类信息
	@RequestMapping(value="/tradeInfo/tree",method=RequestMethod.GET)
	public ModelAndView tradeInfoView(){
		ModelAndView mav = new ModelAndView("/backcenter/tradeInfo/tree");
		return mav;
	}

	
	
	
	@RequestMapping(value="/tradeInfo/tree/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject tradeInfoQuery(@RequestBody JSONObject data)  {
		return tradeInfoService.queryData(data);
	}

	@RequestMapping(value="/tradeInfo/tree/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject tradeInfoSave(@RequestBody JSONObject data)  {
		return tradeInfoService.saveData(data);
	}
	
	@RequestMapping(value="/tradeInfo/tree/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject tradeInfoDelete(@RequestBody JSONObject data) {
		return tradeInfoService.deleteData(data);
	}
	
	@RequestMapping(value="/tradeInfo/tree/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject tradeInfoCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject json = tradeInfoService.newData(data);
		return json;
	}
	
	
	
	
	//应用设置排序
	@RequestMapping(value="/tradeInfo/sortView",method=RequestMethod.GET)
	public ModelAndView tradeInfoSortView(){
		ModelAndView mav = new ModelAndView("/backcenter/tradeInfo/sort");
		return mav;
	}
	//排序
	@RequestMapping(value = "/tradeInfo/sort", method = RequestMethod.POST)
	@ResponseBody
	public void tradeInfoSort(@RequestBody JSONObject data){
		String parentID = data.getString("parentID");
		JSONArray jsonArray =  data.getJSONArray("ids");
//		System.out.println("jsonArray-->"+jsonArray);
		List<String> ids = new ArrayList<String>();
		for(int i=0; i<jsonArray.size();i++){
			ids.add(jsonArray.getString(i));
		}
//		System.out.println("ids-->"+ids);
		tradeInfoService.sortGroupInfo(parentID, ids);
	}

	//选择行业分类
	@RequestMapping(value="/tradeInfo/selectSingle",method=RequestMethod.GET)
	public ModelAndView tradeInfoSelectSingleView(){
		ModelAndView mav = new ModelAndView("/backcenter/tradeInfo/selectSingle");
		return mav;
	}

	//结算信息
	@RequestMapping(value="/settleInfo/list",method=RequestMethod.GET)
	public ModelAndView settleInfoView(){
		ModelAndView mav = new ModelAndView("/backcenter/settleInfo/list");
		return mav;
	}

	


	//快递公司信息
	@RequestMapping(value="/expressInfo/list",method=RequestMethod.GET)
	public ModelAndView expressInfoView(){
		ModelAndView mav = new ModelAndView("/backcenter/expressInfo/list");
		return mav;
	}

	
	
	
	@RequestMapping(value="/expressInfo/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject expressInfoQuery(@RequestBody JSONObject data)  {
		return expressInfoService.queryData(data);
	}

	@RequestMapping(value="/expressInfo/list/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject expressInfoSave(@RequestBody JSONObject data)  {
		return expressInfoService.saveData(data);
	}
	
	@RequestMapping(value="/expressInfo/list/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject expressInfoDelete(@RequestBody JSONObject data) {
		return expressInfoService.deleteData(data);
	}
	
	@RequestMapping(value="/expressInfo/list/create",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject expressInfoCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		JSONObject json = expressInfoService.newData(data);
		return json;
	}
	
	
	@RequestMapping(value="/expressInfo/single",method=RequestMethod.GET)
	public ModelAndView expressInfoSingleView(){
		ModelAndView mav = new ModelAndView("/backcenter/expressInfo/single");
		return mav;
	}



	
}
